package com.cryptoneedle.garden.entity.doris;

import com.cryptoneedle.garden.key.ColumnKey;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Immutable;
import org.hibernate.annotations.Subselect;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;

/**
 * <p>description: Doris-元数据-Column </p>
 * <p>
 * 数据来源：SELECT * FROM information_schema.columns
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "information_schema", name = "columns")
@IdClass(ColumnKey.class)
@Immutable
@Subselect("SELECT * FROM information_schema.columns")
@Comment("Doris-information_schema-columns")
public class DorisInformationColumn implements Serializable {

    // Catalog 名称
    @Id
    @Column(name = "TABLE_CATALOG")
    private String catalogName;

    // Database 名称
    @Id
    @Column(name = "TABLE_SCHEMA")
    private String databaseName;

    // 表名称
    @Id
    @Column(name = "TABLE_NAME")
    private String tableName;

    // 列名称
    @Id
    @Column(name = "COLUMN_NAME")
    private String columnName;

    // 列在表中所处的位置
    @Column(name = "ORDINAL_POSITION")
    private Long ordinalPosition;

    // 列的默认值
    @Column(name = "COLUMN_DEFAULT")
    private String columnDefault;

    // 是否允许为 NULL
    //@Column(name = "IS_NULLABLE")
    //private String isNullable;

    // 数据类型
    @Column(name = "DATA_TYPE")
    private String dataType;

    // 字符类型允许的最大字符数
    @Column(name = "CHARACTER_MAXIMUM_LENGTH")
    private Long characterMaximumLength;

    // 字符类型允许的最大字节数
    @Column(name = "CHARACTER_OCTET_LENGTH")
    private Long characterOctetLength;

    // 数值类型的 Precision
    @Column(name = "NUMERIC_PRECISION")
    private Long numericPrecision;

    // 数值类型的 Scale
    @Column(name = "NUMERIC_SCALE")
    private Long numericScale;

    // datetime 类型的 Precision
    @Column(name = "DATETIME_PRECISION")
    private Long datetimePrecision;

    // 字符类型的字符集名称，永远为 NULL
    //@Column(name = "CHARACTER_SET_NAME")
    //private String characterSetName;

    // 字符类型的排序算法名称，永远为 NULL
    //@Column(name = "COLLATION_NAME")
    //private String collationName;

    // 列的类型
    @Column(name = "COLUMN_TYPE")
    private String columnType;

    // 如果是 UNI，则表示当前列是 Unique Key 列
    @Column(name = "COLUMN_KEY")
    private String columnKey;

    // 列的一些额外信息。包括展示是否为自增列，是否为 Generated 列等
    @Column(name = "EXTRA")
    private String extra;

    // 永远为空
    //@Column(name = "PRIVILEGES")
    //private String privileges;

    // 列的备注信息
    @Column(name = "COLUMN_COMMENT")
    private String columnComment;

    // 列的宽度
    @Column(name = "COLUMN_SIZE")
    private Long columnSize;

    // 数值类型的小数位数
    @Column(name = "DECIMAL_DIGITS")
    private Long decimalDigits;

    // 永远为 NULL
    //@Column(name = "GENERATION_EXPRESSION")
    //private String generationExpression;

    // 永远为 NULL
    //@Column(name = "SRS_ID")
    //private Long srsId;

    public String getKey() {
        return new ColumnKey(this.catalogName, this.databaseName, this.tableName, this.columnName).toString();
    }
}